-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[ADT] Wrapper for std::accumulate
accepting a range
.
#158702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mtrofin
merged 1 commit into
main
from
users/mtrofin/09-15-_adt_wrapper_for_std_accumulate_accepting_a_range_
Sep 16, 2025
Merged
[ADT] Wrapper for std::accumulate
accepting a range
.
#158702
mtrofin
merged 1 commit into
main
from
users/mtrofin/09-15-_adt_wrapper_for_std_accumulate_accepting_a_range_
Sep 16, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-llvm-adt Author: Mircea Trofin (mtrofin) ChangesFull diff: https://github.com/llvm/llvm-project/pull/158702.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 099bef288b953..155feb90bd94c 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -35,6 +35,7 @@
#include <iterator>
#include <limits>
#include <memory>
+#include <numeric>
#include <optional>
#include <tuple>
#include <type_traits>
@@ -1694,6 +1695,11 @@ template <typename R> constexpr size_t range_size(R &&Range) {
return static_cast<size_t>(std::distance(adl_begin(Range), adl_end(Range)));
}
+/// Wrapper for std::accumulate.
+template <typename R, typename E> E range_accumulate(R &&Range, E Init) {
+ std::accumulate(Range.begin(), Range.end(), Init);
+}
+
/// Provide wrappers to std::for_each which take ranges instead of having to
/// pass begin/end explicitly.
template <typename R, typename UnaryFunction>
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index 9fda0d912a2f5..ff1d413979bdc 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -1602,6 +1602,17 @@ TEST(STLExtrasTest, Fill) {
EXPECT_THAT(V2, ElementsAre(Val, Val, Val, Val));
}
+TEST(STLExtrasTest, Accumulate) {
+ EXPECT_EQ(range_accumulate(std::vector<int>(), 0), 0);
+ EXPECT_EQ(range_accumulate(std::vector<int>(), 3), 3);
+ std::vector<int> V1 = {1, 2, 3, 4, 5};
+ EXPECT_EQ(range_accumulate(V1, 0), std::accumulate(V1.begin(), V1.end(), 0));
+ EXPECT_EQ(range_accumulate(V1, 10),
+ std::accumulate(V1.begin(), V1.end(), 10));
+ EXPECT_EQ(range_accumulate(drop_begin(V1), 7),
+ std::accumulate(V1.begin(), V1.end(), 7) - V1[0]);
+}
+
struct Foo;
struct Bar {};
|
kuhar
reviewed
Sep 15, 2025
kuhar
reviewed
Sep 15, 2025
dc3518f
to
454d85e
Compare
5713917
to
588913f
Compare
kazutakahirata
approved these changes
Sep 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
kuhar
approved these changes
Sep 15, 2025
kuhar
reviewed
Sep 15, 2025
588913f
to
277dcd5
Compare
felipepiovezan
pushed a commit
to felipepiovezan/llvm-project
that referenced
this pull request
Oct 20, 2025
(cherry picked from commit 7e71877)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.